package com.fenghuajueli.module_user.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.libbasecoreui.listener.OnBaseClick;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;
import com.fenghuajueli.module_user.R;

import java.util.ArrayList;
import java.util.List;

public class AvatarAdapter extends RecyclerView.Adapter<AvatarAdapter.ViewHolder> {

    private final OnBaseClick<String> picklistener;
    private final List<String> imgUrlList = new ArrayList<>();

    public AvatarAdapter(List<String> imgUrlList, OnBaseClick<String> picklistener) {
        this.imgUrlList.addAll(imgUrlList);
        this.picklistener = picklistener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.module_user_item_avatar, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ImageLoadManager.loadForImageView(holder.avatar, imgUrlList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != picklistener) {
                    picklistener.onClick(imgUrlList.get(holder.getAbsoluteAdapterPosition()));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return imgUrlList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final ImageView avatar;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.ivAvatar);
        }
    }
}